package hhh.app.presenter.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import hhh.app.R;

/**
 * Created by hhh on 2017/4/26.
 */
public class MyEditText extends RelativeLayout {
    ImageView img;
    ImageView rimg;
    EditText editText;

    public MyEditText(Context context) {
        super(context);
        init(null);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        LayoutInflater.from(getContext()).inflate(R.layout.view_myedittext,this);
        img= (ImageView) findViewById(R.id.myedittext_img);
        rimg= (ImageView) findViewById(R.id.myedittext_rimg);
        editText= (EditText) findViewById(R.id.myedittext_edit);

//        TypedArray a=getResources().obtainAttributes(attrs,R.styleable.MyEditText);
        TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MyEditText);
        int count=a.getIndexCount();
        for(int i=0;i<count;i++){
            int index=a.getIndex(i);
            switch (index){
                case R.styleable.MyEditText_img:
                    setImage(img,a.getDrawable(index));
                    break;
                case R.styleable.MyEditText_rimg:
                    setImage(rimg,a.getDrawable(index));
                    break;
                case R.styleable.MyEditText_hint:
                    setHint(a.getString(index));
                    break;
                case R.styleable.MyEditText_img_show:
                    showView(img,a.getInt(index,2));
                    break;
                case R.styleable.MyEditText_rimg_show:
                    showView(rimg,a.getInt(index,2));
                    break;
                case R.styleable.MyEditText_encrypt:
                    setEncrypt(a.getBoolean(index,false));
                    break;
            }
        }
        a.recycle();
    }

    public void setEncrypt(boolean b) {
        if(b) {
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }else{
            editText.setInputType(InputType.TYPE_CLASS_TEXT);
        }
    }

    public void showView(View v, int a) {
        switch (a){
            case 0:
                v.setVisibility(View.VISIBLE);
                break;
            case 1:
                v.setVisibility(View.INVISIBLE);
                break;
            case 2:
                v.setVisibility(View.GONE);
                break;
        }
    }

    public void setHint(String s) {
        editText.setHint(s);
    }

    private void setImage(ImageView img, Drawable drawable) {
        img.setImageDrawable(drawable);
    }

    public void setImg(int id){
        setImage(img,getResources().getDrawable(id));
    }

    public void setRimg(int id){
        setImage(rimg,getResources().getDrawable(id));
    }

}
